package chu.dejian.ui.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.adapter.AttentionAdapter;
import com.sam.blemodule.R;
import chu.dejian.entity.ContactEntity;

import com.sam.ui.base.activity.BaseActivity;
import com.sam.ui.base.fragment.BaseFragment;
import com.sam.widget.headerBar.BaseHeaderView;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;

public class AttentionContactsActivity extends BaseActivity {
    /**获取库Phone表字段**/
    private static final String[] PHONES_PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };

    /**联系人显示名称**/
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;
    /**电话号码**/
    private static final int PHONES_NUMBER_INDEX = 1;

    private ListView contactListView;
    private List<ContactEntity> contactList;

    @Override
    protected void onCreate(@Nullable Intent savedInstanceStateIntent) {
        setContentView(R.layout.fragment_attention_contacts);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final ContactEntity contactEntity = contactList.get(position);
            Intent data = new Intent();
            data.putExtra(AttentionApplyActivity.EXTRA_KEY_CONTACT_NAME, contactEntity.contactName);
            data.putExtra(AttentionApplyActivity.EXTRA_KEY_CONTACT_PHONE, contactEntity.contactPhoneNum);
            data.putExtra(AttentionApplyActivity.EXTRA_KEY_CONTACT_LEVEL, contactEntity.attentionLevel == null ? -1 : contactEntity.attentionLevel.ordinal());
            setResult(RESULT_OK,data);
            finish();
        }
    };

    @Override
    public void findView() {
        contactListView = (ListView) findViewById(R.id.contact_list);
        contactListView.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public void initView() {
        contactList = getContactList();
        contactListView.setAdapter(new AttentionAdapter(this,R.layout.row_attention_horizontal,contactList,true));
    }

    /**
     * 得到手机通讯录联系人信息
     **/
    private List<ContactEntity> getContactList() {
        List<ContactEntity> contactEntities = new ArrayList<>();
        // 获取手机联系人
        Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
        if (phoneCursor != null) {
            while (phoneCursor.moveToNext()) {
                //得到手机号码
                String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
                //当手机号码为空的或者为空字段 跳过当前循环
                if (TextUtils.isEmpty(phoneNumber))continue;
                //得到联系人名称
                String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);

                contactEntities.add(new ContactEntity(contactName,phoneNumber));
            }
            phoneCursor.close();
        }
        return contactEntities;
    }



}
